﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ThreeByte.ActiveDeck.Data.UserResponse.Report {
    
    [IndividualUserData(ShortTypeName="Categories")]
    public class CategoriesIndividualData : IndividualUserData {
       
        public CategoriesIndividualData(XElement node) : this() {
            Section = int.Parse(node.Attribute("Section").Value);
            Page = int.Parse(node.Attribute("Page").Value);

            Words = new Dictionary<int, int>();
            foreach(XElement w in node.Elements(ns + "Word")) {
                Tuple<Word,Category> wordCat = new Tuple<Word,Category>(new Word(), new Category());
                wordCat.Item1.ID = int.Parse(w.Attribute("ID").Value);
                wordCat.Item2.ID = int.Parse(w.Attribute("CategoryID").Value);
                CategoriesWords.Add(wordCat);
            }
        }

        public CategoriesIndividualData() {
            CategoriesWords = new List<Tuple<Word, Category>>();
        }

        public CategoriesIndividualData(int section, int page) {
            Section = section;
            Page = page;
            Words = new Dictionary<int, int>();
            CategoriesWords = new List<Tuple<Word, Category>>();
        }

        public Dictionary<int, int> Words {
            get;
            set;
        }

        public List<Tuple<Word, Category>> CategoriesWords {
            get;
            set;
        }

        public override bool UpdateUserData(IndividualUserData newUserData) {
            throw new NotImplementedException();
        }

        public override XElement ToXml() {
            XElement node = new XElement(ns + "Categories", new XAttribute("Section", Section), new XAttribute("Page", Page), new XAttribute("Prompt", Question == null ? "" : Question));

            foreach(Tuple<Word, Category> wc in CategoriesWords) {
                XElement word = new XElement(ns + "Word", new XAttribute("ID", wc.Item1.ID), new XAttribute("WordText", wc.Item1.Text == null ? "" : wc.Item1.Text), 
                    new XAttribute("CategoryID", wc.Item2.ID), new XAttribute("CategoryText", wc.Item2.Text == null ? "" : wc.Item2.Text), new XAttribute("IsCorrect", wc.Item1.IsCorrect.ToString()));
                node.Add(word);
            }

            return node;
        }

        public class Word {

            public int ID {
                get;
                set;
            }

            public string Text {
                get;
                set;
            }

            private bool _isCorrect = false;
            public bool IsCorrect {
                get {
                    return _isCorrect;
                }
                set {
                    _isCorrect = value;
                }
            }

            public Word() {
               
            }
        }

        public class Category {
            public int ID {
                get;
                set;
            }

            public string Text {
                get;
                set;
            }

            public Category() {
           
            }
        }
    }
}
